/***********************************************************************************************************************
*                                                                                                                      *
* SPLASH build system v0.1                                                                                             *
*                                                                                                                      *
* Copyright (c) 2013 Andrew D. Zonenberg                                                                               *
* All rights reserved.                                                                                                 *
*                                                                                                                      *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the     *
* following conditions are met:                                                                                        *
*                                                                                                                      *
*    * Redistributions of source code must retain the above copyright notice, this list of conditions, and the         *
*      following disclaimer.                                                                                           *
*                                                                                                                      *
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       *
*      following disclaimer in the documentation and/or other materials provided with the distribution.                *
*                                                                                                                      *
*    * Neither the name of the author nor the names of any contributors may be used to endorse or promote products     *
*      derived from this software without specific prior written permission.                                           *
*                                                                                                                      *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES        *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR       *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE       *
* POSSIBILITY OF SUCH DAMAGE.                                                                                          *
*                                                                                                                      *
***********************************************************************************************************************/

#include "splashcore.h"

using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction

BinaryFirmwareNode::BinaryFirmwareNode(
	BuildGraph* graph,
	CppExecutableNode* input,
	vector<string> sections,
	CppToolchain* toolchain,
	string name)
	: BuildGraphNode(graph)
	, m_exe(input)
	, m_sections(sections)
	, m_toolchain(toolchain)
	, m_name(name)
{	
}

BinaryFirmwareNode::~BinaryFirmwareNode()
{
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Dependency scanning

string BinaryFirmwareNode::GetHash()
{
	DebugPrintfHeading(4, "BinaryFirmwareNode::GetHash() [node = \"%s\"]\n", m_name.c_str());
	string hashin = "";
	
	//Update our dependencies
	UpdateDependencies();
	
	//Add data at this node
	hashin += sha256(GetOutputPath());	//necessary to ensure static/shared/exe have different hashes
	hashin += m_toolchain->GetHash();
	for(size_t i=0; i<m_sections.size(); i++)
		hashin += sha256(m_sections[i]);
	
	//Add data for our dependencies
	for(size_t i=0; i<m_dependencies.size(); i++)
		hashin += m_dependencies[i]->GetHashCached(); 
	
	return sha256(hashin);
}

void BinaryFirmwareNode::UpdateDependencies()
{
	DebugPrintfHeading(4, "BinaryFirmwareNode::UpdateDependencies() [node = \"%s\"]\n", m_name.c_str());
	m_dependencies.clear();
	
	m_dependencies.push_back(m_exe);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Compilation

string BinaryFirmwareNode::GetOutputPath()
{
	return m_graph->GetOutDir() + "/" + m_toolchain->GetArchitecture() + "/" + m_name + ".bin";
}

string BinaryFirmwareNode::GetDescription()
{
	char desc[1024];
	snprintf(desc, sizeof(desc), "Binary firmware image %s [%s]", m_name.c_str(), m_toolchain->GetArchitecture().c_str());
	return string(desc);
}

string BinaryFirmwareNode::GetBinaryPath()
{
	return m_graph->GetMetaDir() + "/" + GetHashCached()  + "/" + m_name + ".bin";
}

void BinaryFirmwareNode::Update(Cluster* cluster)
{
	//Rebuild all of our dependencies if necessary
	vector<ClusterJob*> deps;
	for(size_t i=0; i<m_dependencies.size(); i++)
	{		
		BuildGraphNode* node = m_dependencies[i];
		node->UpdateCached(cluster);
		if(node->GetBuildJob() != NULL)
			deps.push_back(node->GetBuildJob());
	}		
		
	//Dispatch the objdump command
	m_job = m_toolchain->DumpSectionsFromExecutable(
		m_exe->GetBinaryPath(),
		GetBinaryPath(),
		m_sections,
		deps,
		cluster);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory method

BinaryFirmwareNode* BinaryFirmwareNode::CreateBinaryFirmwareNode(
	BuildGraph* graph,
	CppExecutableNode* input,
	vector<string> sections,
	CppToolchain* toolchain,
	string name,	
	bool target)
{
	string arch = toolchain->GetArchitecture();
	
	//Add the executable node
	BinaryFirmwareNode* exenode = new BinaryFirmwareNode(graph, input, sections, toolchain, name);
	graph->AddNodeWithPath(exenode, exenode->GetOutputPath(), arch);
	
	//Add it as a top-level target if requested
	if(target)
		graph->AddTarget(exenode, exenode->GetTargetName());
	
	//Done
	return exenode;
}

string BinaryFirmwareNode::GetTargetName() const
{
	return m_name + "-" + m_toolchain->GetArchitecture();
}
